import sys
from os.path import join, dirname

from loguru import logger
from config import settings


class Logger:
    def __init__(
            self,
            std_level=settings.log.std_level,
            file_level=settings.log.file_level,
    ):
        self._logger = logger
        if std_level and file_level:
            log_config = {
                "handlers": [
                    {
                        "sink": sys.stdout,
                        "level": std_level,
                    },
                    {
                        "sink": join(dirname(__file__), 'log_files',"{time:YYYY-MM-DD}.log"),  # log文件的路径
                        "format": "{time:YYYY-MM-DD HH:mm:ss} | {level} | {file}:{line} | {message}",
                        "enqueue": True,
                        "rotation": '00:00',  # 每天 0 点创建一个文件
                        "level": file_level,
                        "encoding": "utf-8",
                    },
                ]
            }
            self._logger.configure(**log_config)

    @property
    def logger(self):
        return self._logger


if __name__ == '__main__':
    Logger().logger.info("abc")
